layui.use(['form','layer','laydate','table','util'],function (){
    let form = layui.form,layer = layui.layer,laydate = layui.laydate,table = layui.table,util = layui.util;
    let dateElem = $('.form-dateElem');
    //渲染日期模块，最大日期为当前日期
    layui.each(dateElem,function () {
        laydate.render({
            elem:this,
            //最大日期比今天多几天
            max:0,
        })
    })

    //监听table，修改捆号、页数
    table.on('edit(fistDataTable)',function (obj) {
        //获取原始值
        let oldValue = $(this).prev().text();
        // 获取所在行的相关数据中的受理号数据
        let atBussId = obj.data.atBussId;
        let jsonData = {};
        //当前单元格的字段名称
        let field = obj.field;
        if (valueIsEqual("atBundleNo",field)){
            //当前单元格编辑之后的数据
            let atBundleNo = obj.value;
            jsonData = {
                "atBundleNo":atBundleNo,
                "atBussId":atBussId
            }
            //正则表达式 匹配的格式 350200-20210923-00001
            let reqExp = /^(35020\d)-((19|20)\d{6})-(\d{5})$/;
            //捆号可以为空
            if (!valueIsEmpty(atBundleNo) && !reqExp.exec(atBundleNo)){
                top.layer.alert("捆号格式不符合要求，请按照页面提示填写！",function (index) {
                    obj.update({
                        'atBundleNo':oldValue
                    });
                    top.layer.close(index);
                });
                return;
            }
        }
        if (valueIsEqual("atAresTolNum",field)){
            let atAresTolNum = obj.value;
            let reqExp = /^[1-9]\d*$/;
            if (!reqExp.exec(atAresTolNum)){
                top.layer.alert("输入的页数不符合要求，请重新输入！",function (index) {
                    obj.update({
                        'atAresTolNum':valueClean(oldValue,"页")
                    });
                    top.layer.close(index);
                });
                return;
            }
            jsonData = {
                "atAresTolNum":atAresTolNum,
                "atBussId":atBussId
            }
        }
        sendAjax("/business/acceptAdmin/updateAcceptInfo",jsonData,function (result) {
            //获取返回的数据，如果为1，则说明修改了1条数据
            let resultData = result.code;
            if (valueIsEqual("00",resultData)){
                top.layer.alert(result.msg,{title:'Notice'});
            }else {
                if (valueIsEqual('atAresTolNum',field)) {
                    top.layer.alert(result.msg, function (index) {
                        obj.update({
                            'atAresTolNum': valueClean(oldValue, "页")
                        })
                        top.layer.close(index);
                    })
                }
                if (valueIsEqual('atBundleNo',field)){
                    top.layer.alert(result.msg, function (index) {
                        obj.update({
                            'atBundleNo': oldValue
                        })
                        top.layer.close(index);
                    })
                }
            }
        })
    });
    //监听table 删除
    table.on('tool(fistDataTable)',function (obj) {
        let layEvent = obj.event;
        if (valueIsEqual(layEvent,"delete")) {
            let atBussId = obj.data.atBussId;
            let jsonData = {
                "atBussId":atBussId
            }
            sendAjax("/business/acceptAdmin/deleteAcceptInfo",jsonData,function (result) {
                let rsltData = result.code;
                if (valueIsEqual("00",rsltData)){
                    top.layer.alert("删除受理信息成功!",function (index) {
                        //删除对应行（tr）的DOM结构，并更新缓存
                        obj.del();
                        top.layer.close(index);
                        /*//表格重载
                        table.reload('dataTableId',{
                           where:{
                               "srchReqFir" : $("select[name = 'srchReqFir']").val(),
                               "qryReqFir" : $("input[name = 'qryReqFir']").val().trim(),
                               "stDt" : $('#stDt').val(),
                               "lmDt" : $('#lmDt').val()
                           },
                            page: {
                               curr:1
                            }
                        });*/
                        selectAcceptArchives();
                    });
                }else {
                    top.layer.alert(result.msg);
                }
            })
        }
    })
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 初始化生成table数据
*/
$(function () {
    selectAcceptArchives();
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/22
* @description: 生成table数据
*/
function selectAcceptArchives() {
    //起始日期
    let stDt = $('#stDt').val();
    //截止日期
    let lmDt = $('#lmDt').val();
    //查询条件类型
    let srchReqFir = $("select[name = 'srchReqFir']").val();
    //查询条件
    let qryReqFir = $("input[name = 'qryReqFir']").val().trim();

    let stDtClean = valueClean(stDt,"-");
    let lmDtClean = valueClean(lmDt,"-");
    if (!valueIsEmpty(stDt) && ! valueIsEmpty(lmDt) && stDtClean > lmDtClean){
        top.layer.alert("起始日期不能大于截止日期");
        return;
    }
    let jsonData = {
        "srchReqFir" : srchReqFir,
        "qryReqFir" : qryReqFir,
        "stDt" : stDt,
        "lmDt" : lmDt
    }
    //时间戳
    let tableTitle = '企业受理信息表'+layui.util.toDateString(new Date(),'yyyyMMddHHmmss');
    layui.table.render({
        elem:'#dataTable',
        id:'dataTableId',
        url:'/business/acceptAdmin/selectAcceptInfo',
        parseData:function (result) {
            return{
                "code":result.code,
                "msg":result.msg,
                "count":result.data.pageCount,
                "data":result.data.list
            }
        },
        size:'sm',
        //分页的起止页号，每页条数
        page:{
            curr:1,
            limit:15,
            limits:[15,30,50,100]
        },
        //自定义的页码参数名称、每页条数参数名称
        request:{
            pageName:'pageNum',
            limitName:'pageSize'
        },
        //table的高度
        height:550,
        //查询条件，json格式
        where:jsonData,
        cols:[[
            {type:'checkbox'},
            {type:'numbers',title:'序号'},
            {field:'atBundleNo',title:'捆号',edit:'text'},
            {field:'atAcptId',title:'受理号'},
            {field:'atCompNm',title:'企业名称',width:300},
            {field:'atAcpterNm',title:'受理人',width:100},
            {field:'atAcptDt',title:'受理日期',width: 100,templet:function (d) {
                    return layui.util.toDateString(d.atAcptDt,"yyyy-MM-dd");
                }},
            {field:'atRegistId',title:'注册号',width: 150},
            {field:'atCreditCode',title:'统一社会信用代码'},
            {field:'atArchType',title:'档案类型',width:100},
            {field:'atAcptStdNm',title:'受理状态',width: 100},
            {field:'atAresTolNum',title:'档案页数',edit:'text',width: 100,templet:function (d){
                return d.atAresTolNum+'页';
                }},
            {title:'操作',fixed:'right',width:60,align:'center',toolbar:'#toolBar'},
            {field:'atBussId',title:'业务申请编号',hide:true}
        ]],
        //头部工具条
        toolbar: true,
        //导出时候excel文件标题
        title:tableTitle

    });
}